package com.dayuanit.dy17.atm.service.impl;

import com.dayuanit.dy17.atm.config.AtmPropertiesConfig;
import com.dayuanit.dy17.atm.dto.ResponseDTO;
import com.dayuanit.dy17.atm.entity.User;
import com.dayuanit.dy17.atm.entity.WxBind;
import com.dayuanit.dy17.atm.exception.BizException;
import com.dayuanit.dy17.atm.mapper.UserMapper;
import com.dayuanit.dy17.atm.mapper.WxBindMapper;
import com.dayuanit.dy17.atm.service.IUserService;
import com.dayuanit.dy17.atm.util.SignUtils;
import org.apache.commons.codec.digest.DigestUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class UserServiceImpl implements IUserService {

    @Autowired
    private UserMapper userMapper;

    @Autowired
    private AtmPropertiesConfig atmPropertiesConfig;

    @Autowired
    private SignUtils signUtils;

    @Autowired
    private WxBindMapper wxBindMapper;

    @Override
    public User login(String username, String password) {
        if (StringUtils.isBlank(username)) {
            throw new BizException("请输入用户名");
        }

        if (StringUtils.isBlank(password)) {
            throw new BizException("请输入密码");
        }

        User user = userMapper.getByUserName(username);
        if (null == user) {
            throw new BizException("用户名不存在");
        }

        if (!signUtils.checkLoginPasswordSign(username, password, user.getPassword())) {
            throw new BizException("密码错误");
        }

        //TODO 可能还有一些安全风控的校验 比如用户是否被拉黑 是否被禁止登录等。
        return user;
    }

    @Override
    public void register(String username, String password, String confirmPassword) {
        if (StringUtils.isBlank(username)) {
            throw new BizException("请输入用户名");
        }

        if (StringUtils.isBlank(password)) {
            throw new BizException("请输入密码");
        }

        if (StringUtils.isBlank(confirmPassword)) {
            throw new BizException("请输入确认密码");
        }

        if (!password.equals(confirmPassword)) {
            throw new BizException("两次输入的密码不匹配");
        }

        User user = userMapper.getByUserName(username);
        if (null != user) {
            throw new BizException("用户已存在");
        }

        user = new User();
        user.setPassword(signUtils.createLoginPasswordSign(password, username));
        user.setUsername(username);
        int rows = userMapper.insert(user);
        if (1 != rows) {
            throw new BizException("注册失败");
        }
    }

    @Override
    public void bindEmail(long userId, String email, String sign, long createTime) {
        User user = userMapper.selectByPrimaryKey(userId);

        if (!signUtils.checkActiveEmailSign(email, user.getUsername(), createTime, sign)) {
            throw new BizException("非法请求");
        }

        // 查看邮箱是否已经绑定
        int count = userMapper.countByEmail(email);
        if (count > 0) {
            throw new BizException("邮箱已绑定");
        }

        user.setEmail(email);

        int rows = userMapper.updateByPrimaryKey(user);
        if (1 != rows) {
            throw new BizException("绑定失败");
        }
    }

    @Override
    public void resetPwd(String password, String confimPwd, String bindEmail, String sign, long createTime) {
        if (StringUtils.isBlank(password) || StringUtils.isBlank(confimPwd)) {
            throw new BizException("请输入必填参数");
        }

        if (StringUtils.isBlank(bindEmail) || StringUtils.isBlank(sign)) {
            throw new BizException("非法请求");
        }

        //校验签名
        if (!signUtils.checkRestPwdSign(bindEmail, sign, createTime)) {
            throw new BizException("非法请求");
        }

        // 请求是否过期
        if (atmPropertiesConfig.getResetPwdExpire() * 1000 + createTime < System.currentTimeMillis() ) {
            throw new BizException("请求已过期");
        }

        // 通过邮箱找到用户
        User user = userMapper.getByEmail(bindEmail);
        if (null == user) {
            throw new BizException("邮箱不存在");
        }

        user.setPassword(signUtils.createLoginPasswordSign(password, user.getUsername()));

        int row = userMapper.updateByPrimaryKey(user);
        if (1 != row) {
            throw new BizException("重置密码失败");
        }
    }

//    @Override
//    public void wxLoginBind(String username, String password, WxUserInfoDomain wxUserInfoDomain) {
//        if (StringUtils.isBlank(username) || StringUtils.isBlank(password)) {
//            throw new BizException("有必填参数为空");
//        }
//
//        User user = userMapper.getByUserName(username);
//        if (null == user) {
//            throw new BizException("用户名不存在");
//        }
//
//        if (!signUtils.checkLoginPasswordSign(username, password, user.getPassword())) {
//            throw new BizException("密码不正确");
//        }
//
//        WxBind wxBind = new WxBind();
//        wxBind.setHeadimgurl(wxUserInfoDomain.getHeadimgurl());
//        wxBind.setNickname(wxUserInfoDomain.getNickname());
//        wxBind.setOpenId(wxUserInfoDomain.getOpenid());
//        wxBind.setProvince(wxUserInfoDomain.getProvince());
//        wxBind.setUnionid(wxUserInfoDomain.getUnionid());
//        wxBind.setUserId(user.getId());
//        wxBindMapper.insert(wxBind);
//    }

    @Override
    public WxBind getWxbind(String openId) {
        return wxBindMapper.getByOpenId(openId);
    }

    @Override
    public User getUser(long userId) {
        return userMapper.selectByPrimaryKey(userId);
    }
}
